1 //------------------------------------------------------------------------------
2 // <copyright file="emit.cs" company="Microsoft">
4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
14 //------------------------------------------------------------------------------
19 * the emit routines add "instruction" to the instruction stream (istream)
20 * currently the istream is flushed when the emitAsm routine is called
23 using System
.Reflection
;
27 IAsm iroot
; // root of current instruction list
28 IAsm icur
; // current insn ptr
29 IAsm LastComment
; // ptr to last comment placeholder
31 VarList localvars
; // local copy of current localvar list
33 Io io
; // local copy of current IO handle
35 Exe exe
; // instance of exe class for generation
38 * get the function definition entry
48 if (a
.getIType() == IAsm
.I_FUNC_BEGIN
)
52 return null; /* didn't find anything */
55 void NextInsn(int incr
)
60 icur
= iroot
= new IAsm();
64 ncount
= icur
.getICount() + incr
; /* propogate previous count */
65 icur
.setNext(new IAsm());
66 icur
= icur
.getNext();
68 icur
.setICount(ncount
);
72 * Emit a field def to ilist
74 public void FieldDef(Var e
)
77 icur
.setIType(IAsm
.I_FIELD
);
82 * Emit function begin to ilist
84 public void FuncBegin(Var e
)
87 icur
.setIType(IAsm
.I_FUNC_BEGIN
);
92 * Emit the local declarations
94 public void LocalVars(VarList v
)
98 icur
.setIType(IAsm
.I_LOCALDEF
);
102 * Emit instruction to ilist
104 public void Insn(String s
)
107 icur
.setIType(IAsm
.I_INSN
);
111 public void Label(String lname
)
112 { // this is the branch target
114 icur
.setIType(IAsm
.I_LABEL
);
115 icur
.setLabel(lname
);
118 public void Branch(String s
, String lname
)
119 { // this is the branch source
121 icur
.setIType(IAsm
.I_BRANCH
);
123 icur
.setLabel(lname
);
126 public void Store(Var e
)
129 icur
.setIType(IAsm
.I_INSN_STORE
);
133 public void Load(Var e
)
136 icur
.setIType(IAsm
.I_INSN_LOAD
);
140 public void LoadConst(String s
)
143 icur
.setIType(IAsm
.I_INSN_LOAD_CONST
);
147 public void Call(Var e
)
150 icur
.setIType(IAsm
.I_CALL
);
151 icur
.setVar(e
); /* this is the callname */
159 public void CommentHolder()
162 icur
.setIType(IAsm
.I_COMMENT
);
163 LastComment
= icur
; /* save away this insn loc to store comment */
164 icur
.setCommentLine(io
.commentGetCurrentLine());
167 public void CommentFill(String comment
)
170 Console
.Write("CommentFill S=");
171 for (int _debug_i
=0; _debug_i
<comment
.Length
;_debug_i
++)
173 int _debug_d
= comment
[_debug_i
];
174 char _debug_c
= (char) (_debug_d
+ 96);
176 Console
.Write("^"+_debug_c
);
178 Console
.Write(comment
[_debug_i
]);
180 Console
.Write(_debug_d
);
183 Console
.WriteLine(";");
185 if (LastComment
!= null)
186 LastComment
.setComment(comment
);
192 icur
.setIType(IAsm
.I_RET
);
195 public void FuncEnd()
198 icur
.setIType(IAsm
.I_FUNC_END
);
202 * Emit exe instructions now
203 * this flushes the istream
212 switch (a
.getIType())
223 case IAsm
.I_INSN_STORE
:
226 case IAsm
.I_INSN_LOAD
:
229 case IAsm
.I_INSN_LOAD_CONST
:
232 case IAsm
.I_FUNC_BEGIN
:
233 exe
.FuncBegin(a
); /* Emit function beginning */
235 case IAsm
.I_FUNC_END
:
247 case IAsm
.I_LOCALDEF
:
248 exe
.LocalVars(localvars
);
254 io
.Abort("Unhandled instruction type " + a
.getIType());
263 * Emit assembly instructions now
264 * this flushes the istream
274 switch (a
.getIType())
285 case IAsm
.I_INSN_STORE
:
288 case IAsm
.I_INSN_LOAD
:
291 case IAsm
.I_INSN_LOAD_CONST
:
294 case IAsm
.I_FUNC_BEGIN
:
295 x
.FuncBegin(a
); /* Emit function beginning */
297 case IAsm
.I_FUNC_END
:
312 case IAsm
.I_LOCALDEF
:
313 x
.LocalVars(localvars
);
316 io
.Abort("Unhandled instruction type " + a
.getIType());
324 public void BeginModule()
326 exe
.BeginModule(io
.GetInputFilename());
329 public void BeginClass()
331 exe
.BeginClass(Io
.GetClassname(), TypeAttributes
.Public
);
333 io
.Out(".class " + Io
.GetClassname() + "{\r\n");
336 public void EndClass()
343 public void EndModule()
351 exe
= new Exe(Io
.GetClassname());